package cn.wolfcode.shop.cloud.config;

import cn.wolfcode.shop.cloud.key.RedisProperties;
import cn.wolfcode.shop.cloud.service.RedisService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
@EnableConfigurationProperties(RedisProperties.class)
//满足条件配置类才会生效
@ConditionalOnProperty(prefix = "redis", name = "host")
public class RedisAutoConfiguration {
    @Bean
    public JedisPool jedisPool(RedisProperties properties) {
        //配置连接池
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxWaitMillis(properties.getPoolMaxWait());
        jedisPoolConfig.setMaxTotal(properties.getPoolMaxTotal());
        jedisPoolConfig.setMaxIdle(properties.getPoolMaxIdle());
        return new JedisPool(jedisPoolConfig, properties.getHost(), properties.getPort(), properties.getTimeout(),properties.getPassword());
    }
//返回值交给spring容器管理
    @Bean
    public RedisService redisService() {
        return new RedisService();
    }


}
